<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>
            <input type="checkbox" v-model="isAll" />
            <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <CartBody
        :goodList="goodList"
        @index11="index22"
        @addFn11="addFn22"
        @subtractFn11="subtractFn22"
        @del11="del22"
      ></CartBody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">{{ total }}</td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import CartBody from "./components/CartBody.vue";
export default {
  components: { CartBody },
  data() {
    return {
      total: "",
      goodList: [
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    };
  },
  computed: {
    isAll: {
      get() {
        //判断数组为空,全选为false
        if (!this.goodList.length) return false;
        //every()遍历当全部都为true,全选才为true
        return this.goodList.every((item) => item.checked);
      },
      set(checked) {
        //全选为true,遍历数组都为true
        this.goodList.forEach((item) => (item.checked = checked));
      },
    },
  },
  methods: {
    //复选框取反赋值
    index22(index) {
      this.goodList[index].checked = !this.goodList[index].checked;
    },
    //数量加加
    addFn22(index) {
      this.goodList[index].num++;
    },
    //数量减减
    subtractFn22(index) {
      if (this.goodList[index].num > 1) {
        this.goodList[index].num--;
      }
    },
    //删除
    del22(index) {
      this.goodList.splice(index, 1);
    },
  },
};
</script>

<style>
table {
  margin: 0 auto;
}
</style>